package Document;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.regex.*;

import com.mysql.jdbc.Statement;

public class Utilisateur {

	private String mail;
	private String prenom;
	private String mdp;
	private String nom;

	
	public void setMdp(String password) {
		this.mdp = password;
	}
	
	public void setMail(String email) {
		this.mail = email;
	}
	
	public void setPrenom(String firstname) {
		this.prenom = firstname;
	}
	
	public void setNom(String name) {
		this.nom = name;
	}
	
	
	private String getMdp() {
		// TODO Auto-generated method stub
		return mdp;
	}
	
	
	private String getMail() {
		// TODO Auto-generated method stub
		return mail;
	}

	public String getPrenom() {
		// TODO Auto-generated method stub
		return prenom;
	}
	
	public String getNom() {
		// TODO Auto-generated method stub
		return prenom;
	}
	
	public void MAJEmail(String Mail, DAO connector, String Email)throws ClassNotFoundException, SQLException 
	{
		String requete="UPDATE inscription SET Mail = '?' WHERE Mail = '/'";
		Connection connexion=connector.getConnexion();		
		PreparedStatement statement=connexion.prepareStatement(requete);
		requete = requete.replace("?", Mail);
		requete = requete.replace("/", Email);
		statement.executeUpdate(requete);
		statement.close();
	}
	
	public double VerifAutorisation (String Mail, DAO connector, String Type) throws ClassNotFoundException, SQLException 
	{
		String requete="SELECT Autorisation FROM inscription WHERE Mail = '?' ";
		String resu = "";
		double nb = 0;
		requete = requete.replace("?", Mail);
		Connection connexion=connector.getConnexion();
		PreparedStatement statement=connexion.prepareStatement(requete);
		ResultSet rs = statement.executeQuery(requete);
		while(rs.next())
		{
		resu = rs.getString("Autorisation");
		}
		
		if(resu.equals("Admin") || (resu.equals("Premium")))
		{
			 String requete2 = " SELECT COUNT(id_template) FROM template WHERE TYPE_TEMPLATE = '?'";
			 requete2 = requete2.replace("?", Type);
			 PreparedStatement statement2=connexion.prepareStatement(requete2);
			 ResultSet rs2 = statement2.executeQuery(requete2);
			 rs2.next();
			 nb=rs2.getDouble(1);
			
		} 
		if(resu.equals("Free"))
		{
			 String requete2 = " SELECT COUNT(id_template) FROM template WHERE TYPE_TEMPLATE = '?'";
			 requete2 = requete2.replace("?", Type);
			 PreparedStatement statement2=connexion.prepareStatement(requete2);
			 ResultSet rs2 = statement2.executeQuery(requete2);
			 rs2.next();
			 nb=(rs2.getDouble(1))/2;
		}
	
	
	
		return nb;
	}
	
	public void MAJMdp(String Mdp, DAO connector, String Mail)throws ClassNotFoundException, SQLException 
	{
		String requete="UPDATE inscription SET Mdp = '?' WHERE Mail ='/' ";
		Connection connexion=connector.getConnexion();		
		PreparedStatement statement=connexion.prepareStatement(requete);
		requete = requete.replace("?", Mdp);
		requete = requete.replace("/", Mail);
		statement.executeUpdate(requete);
		statement.close();
		
	}
	
	public boolean VerificationMail (String Mail)
	{
		boolean test_mail = false;
		String regex = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})$".toString();
		Pattern pattern = Pattern.compile(regex);
		boolean verif = false;
		Matcher matcher = pattern.matcher(Mail);
		verif = matcher.matches();
		if(verif == false)
		{
			test_mail = false;
		}
		else
		{			
			test_mail = true;
		}
		return test_mail;
	}
	public boolean ajouterUtilisateur(DAO connector, String Mail, String Nom, String Prenom, String Mdp) throws ClassNotFoundException, SQLException {
		
		boolean test = false;
		String regex = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})$".toString();
		Pattern pattern = Pattern.compile(regex);
		//String Mail = getMail();
		boolean verif = false;
		
		
		if((getNom() != "")&&(getPrenom() != "")&&(getMdp() != "")&&(getMail()!= ""))
		{	
			Matcher matcher = pattern.matcher(Mail);
			verif = matcher.matches();
			if(verif == false)
			{
				test = false;
			
			}
			else
			{
				String sql = "SELECT Mail FROM inscription WHERE Mail = '?' ";
				String requete="INSERT INTO inscription (Nom, Prenom, Mail, Mdp)  VALUES ('?', '!', '/', '*')";
				Connection connexion=connector.getConnexion();		
				sql = sql.replace("?", Mail);
			    requete = requete.replace("?", Nom);
			    requete = requete.replace("!", Prenom);
			    requete = requete.replace("*", Mdp);
			    requete = requete.replace("/", Mail);
			    PreparedStatement statement1=connexion.prepareStatement(requete);
			    PreparedStatement statement2=connexion.prepareStatement(sql);
			    ResultSet rs = statement2.executeQuery(sql);
			    if(rs.next())
			    {
			    	test= false;
			    	statement2.close();
			    }
			    else
			    {
				statement1.executeUpdate();
				statement1.close();
				test = true;
			    }
				
				
			}
		}
		return test;
	}
	

}
	



